package com.yami.shop.api.manager;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.dto.UnPackRedDto;
import com.yami.shop.bean.event.MeltSendGroupSysMsgEvent;
import com.yami.shop.bean.event.MeltSendOneSysMsgEvent;
import com.yami.shop.bean.model.MeltCloudUserRemark;
import com.yami.shop.bean.model.RedPacketInfo;
import com.yami.shop.bean.model.User;
import com.yami.shop.service.MeltCloudUserRemarkService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class ReadMessage {

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private UserService userService;
    @Autowired
    private MeltCloudUserRemarkService userRemarkService;

//    @Async("sendReadPageMessageExecutor")
    public void sendReadPageMessage(RedPacketInfo byId, UnPackRedDto dto) throws InterruptedException {
        if (byId.getUserId().equals(dto.getUserId())) {
            List<String> userId = Collections.singletonList(byId.getUserId());
            MeltSendGroupSysMsgEvent build = MeltSendGroupSysMsgEvent.builder().tExtra("S:SRpm").targetId(dto.getTargetId()).message("你领取了自己的").toUserId(userId).build();
            applicationContext.publishEvent(build);
        } else {
            User fsUser = userService.getById(byId.getUserId());
            User jsUser = userService.getById(dto.getUserId());
            // 发送者者消息
            List<String> userId = Collections.singletonList(byId.getUserId());
            // 获取备注
            QueryWrapper<MeltCloudUserRemark> meltCloudUserRemarkQueryWrapper = new QueryWrapper<>();
            meltCloudUserRemarkQueryWrapper.eq("type",1);
            meltCloudUserRemarkQueryWrapper.eq("user_id",fsUser.getUserId());
            meltCloudUserRemarkQueryWrapper.eq("obj_id",jsUser.getUserId());
            meltCloudUserRemarkQueryWrapper.last("limit 1");
            MeltCloudUserRemark one = userRemarkService.getOne(meltCloudUserRemarkQueryWrapper);
            MeltSendGroupSysMsgEvent build1 = MeltSendGroupSysMsgEvent.builder()
                    .tExtra("S:SRpm")
                    .targetId(dto.getTargetId())
                    .message((ObjectUtil.isNotEmpty(one) ? one.getRemark() : jsUser.getNickName()) + "领取了你的")
                    .toUserId(userId).build();
            applicationContext.publishEvent(build1);
            // 获取备注
            QueryWrapper<MeltCloudUserRemark> meltCloudUserRemarkQueryWrapper1 = new QueryWrapper<>();
            meltCloudUserRemarkQueryWrapper1.eq("type",1);
            meltCloudUserRemarkQueryWrapper1.eq("user_id",jsUser.getUserId());
            meltCloudUserRemarkQueryWrapper1.eq("obj_id",fsUser.getUserId());
            meltCloudUserRemarkQueryWrapper1.last("limit 1");
            MeltCloudUserRemark one1 = userRemarkService.getOne(meltCloudUserRemarkQueryWrapper1);
            // 接收者消息
            List<String> jsUserId = Collections.singletonList(dto.getUserId());
            MeltSendGroupSysMsgEvent build2 = MeltSendGroupSysMsgEvent.builder()
                    .tExtra("S:SRpm")
                    .targetId(dto.getTargetId())
                    .message("你领取了" + (ObjectUtil.isNotEmpty(one1) ? one1.getRemark() : fsUser.getNickName()) + "的")
                    .toUserId(jsUserId).build();
            applicationContext.publishEvent(build2);
        }
    }

    public void sendReadPrivateChatMessage(RedPacketInfo byId, UnPackRedDto dto) {
        User fsUser = userService.getById(byId.getUserId());
        User jsUser = userService.getById(dto.getUserId());
        // 获取备注
        QueryWrapper<MeltCloudUserRemark> meltCloudUserRemarkQueryWrapper = new QueryWrapper<>();
        meltCloudUserRemarkQueryWrapper.eq("type",1);
        meltCloudUserRemarkQueryWrapper.eq("user_id",fsUser.getUserId());
        meltCloudUserRemarkQueryWrapper.eq("obj_id",jsUser.getUserId());
        meltCloudUserRemarkQueryWrapper.last("limit 1");
        MeltCloudUserRemark one = userRemarkService.getOne(meltCloudUserRemarkQueryWrapper);
        // 发送者者消息
        MeltSendOneSysMsgEvent build1 = MeltSendOneSysMsgEvent.builder()
                .message((ObjectUtil.isNotEmpty(one) ? one.getRemark() : jsUser.getNickName()) + "领取了你的")
                .senderId(jsUser.getUserId())
                .tExtra("S:SRpm")
                .targetId(fsUser.getUserId())
                .IsIncludeSender(0)
                .build();
        applicationContext.publishEvent(build1);
        // 获取备注
        QueryWrapper<MeltCloudUserRemark> meltCloudUserRemarkQueryWrapper1 = new QueryWrapper<>();
        meltCloudUserRemarkQueryWrapper1.eq("type",1);
        meltCloudUserRemarkQueryWrapper1.eq("user_id",jsUser.getUserId());
        meltCloudUserRemarkQueryWrapper1.eq("obj_id",fsUser.getUserId());
        meltCloudUserRemarkQueryWrapper1.last("limit 1");
        MeltCloudUserRemark one1 = userRemarkService.getOne(meltCloudUserRemarkQueryWrapper1);
        // 接收者消息
        MeltSendOneSysMsgEvent build2 = MeltSendOneSysMsgEvent.builder()
                .senderId(fsUser.getUserId())
                .targetId(jsUser.getUserId())
                .message("你领取了" + (ObjectUtil.isNotEmpty(one1) ? one1.getRemark() : fsUser.getNickName()) + "的")
                .tExtra("S:SRpm")
                .IsIncludeSender(0)
                .build();
        applicationContext.publishEvent(build2);
    }
}
